<?php
final class MessageModel {
	private $mysql;
	private $tableName = 'Message';
	public $idRowName = 'Message_ID';
	public $descriptionRowName = 'Message_description';
	public $fromRowName = 'From_Circle_ID';
	public $toRowName = 'To_Circle_ID';
	public $pubDateRowName = 'Message_pubDate';
	public $CountRowName = 'Message_Count';

	public function __construct ($mysql) {
		$this->mysql = $mysql;
	}

/*	public function GetMessageCount ($ids = [], $StartDate, $EndDate) {
		$ref = 0;
		$group = '';
#		foreach ($ids as $id) {
#			$group .= sprintf ('`%s` = 
#		}
#		$sqlresult = $this->mysql->query (sprintf ('SELECT COUNT(*) AS `%s` FROM `%s` WHERE `%s`=%d AND `%s` > \'%s\' AND `%s` < \'%s\';', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->toRowName), $this->mysql->escape_string ($id), $this->mysql->escape_string ($this->pubDateRowName), $StartDate, $this->mysql->escape_string ($this->pubDateRowName), $EndDate));
#		$len = $sqlresult->num_rows ();
#		if ($len > 0) {
#			$data = $sqlresult->fetch_array ();
#			$ref = $data[$this->CountRowName];
#		}
#		$sqlresult->free_result ();
		return $ref;
	}*/
/*
	public function GetMessageCount () {
		$ref = 0;
		$sqlresult = $this->mysql->query (sprintf ('SELECT COUNT(*) AS `%s` FROM `%s`;', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName)));
		$len = $sqlresult->num_rows ();
		if ($len > 0) {
			$data = $sqlresult->fetch_array ();
			$ref = $data[$this->CountRowName];
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function GetMessageItems ($offset, $count) {
		$ref = array ();
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s` FROM `%s` ORDER BY `%s` DESC LIMIT %d, %d;', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($offset), $this->mysql->escape_string ($count)));
		while ($data = $sqlresult->fetch_array ()) {
			$ref[] = $data;
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function DelMessageItem ($id) {
		$len = 0;
		$sqlresult = $this->mysql->query (sprintf ('DELETE FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($id)));
		$len = $sqlresult->affected_rows ();
		$sqlresult->free_result ();
		return $len;
	}

	public function AddMessageItem ($description) {
		$sqlresult = $this->mysql->query (sprintf ('INSERT INTO `%s` (`%s`, `%s`) VALUES (now(), \'%s\');', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($description)));
		$sqlresult->free_result ();
	}
*/
	public function AddMessageItem ($description, $fromid, $toid) {
		$sqlresult = $this->mysql->query (sprintf ('INSERT INTO `%s` (`%s`, `%s`, `%s`, `%s`) VALUES (\'%s\', %d, %d, NOW());', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($this->fromRowName), $this->mysql->escape_string ($this->toRowName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($description), $this->mysql->escape_string ($fromid), $this->mysql->escape_string ($toid)));
		$sqlresult->free_result ();
	}
}
?>
